package backstage.competence.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import backstage.competence.dao.CompetenceDao;
import backstage.competence.pojo.Competence;
import backstage.competence.service.CompetenceService;
import backstage.menu.dao.SysMenuDao;
import backstage.menu.pojo.SysMenu;
import backstage.menu.vo.SysMenuVo;
import backstage.sys.util.Tree;
import backstage.util.pojo.Page;

public class CompetenceServiceImpl implements CompetenceService {

	private CompetenceDao competenceDao;
	private SysMenuDao sysmenuDao;
	

	public Competence getCompetence(Integer id) {
		return competenceDao.getCompetence(id);
	}
	
	//增加
	public void addCompetence(Competence c) {
		competenceDao.seva(c);
	}

	//修改
	public void updateCompetence(Competence c) {
		competenceDao.update(c);
	}
	
	//删除
	public void deleteCompetence(Competence c) {
		competenceDao.delete(c);
	}
	
	//得到页
	public Page getPage(int pageSize) {
		Page p = competenceDao.getPage(pageSize);
		return p;
	}

	//根据分页得到数据
	public List<Competence> getCompetence(Page p) {
		String hql = "from Competence order by " + p.getSortOrder();
		List<Competence> list = competenceDao.getCompetenceList(hql, p);
		return list;
	}
	
	//返回菜单
	public List<SysMenuVo> getMenus(Integer id){
		Competence c = competenceDao.getCompetence(id);
		Set<SysMenu> l = c.getMenus();
		List<SysMenuVo> lm = new ArrayList<SysMenuVo>();
		Iterator<SysMenu> i = l.iterator();
		while(i.hasNext()){
			SysMenu m = (SysMenu)i.next();
			if(m.getState() == 1){
				SysMenuVo vo = new SysMenuVo(m);
				lm.add(vo);
			}
		}
		Tree tree = new Tree();
		List<SysMenuVo> request = tree.getResult(lm);
		return request;
	}
	
	//返回json菜单
	public String getMenusJson(Integer id){
		StringBuffer json = new StringBuffer("{\"request\":[");
		Competence c = competenceDao.getCompetence(id);
		Set<SysMenu> l = c.getMenus();
		
		Map<String,SysMenu> map = new HashMap<String, SysMenu>();
		Iterator<SysMenu> it = l.iterator();
		while(it.hasNext()){
			SysMenu m = (SysMenu)it.next();
			map.put(m.getName(), m);
		}
		List<SysMenu> all = sysmenuDao.getSysMenuList("from SysMenu order by orders", null);
		for (int i = 0; i < all.size(); i++) {
			SysMenu m = all.get(i);
			String name = m.getName();
			json.append("{ \"id\":");
			json.append(m.getLabeling());
			json.append(", \"pId\":");
			json.append(m.getOnlabeling());
			json.append(", \"name\":\"");
			json.append(name);
			if(map.get(name) != null){
				json.append("\", \"checked\":\"true\"},");
			}else{
				json.append("\"},");
			}
		}
		String str = json.substring(0,json.length() - 1) + "]}";
		return str;
	}

	public String updateMenus(String id, String m) {
		if(id != null && !"".equals(id) && m != null && !"".equals(m)){
			Competence c = competenceDao.getCompetence(new Integer(id));
			String hql = "from SysMenu ";
			String[] values = m.split(",");
			for (int i = 0; i < values.length; i++) {
				if(i==0){
					hql+=" where labeling=?";
				}else{
					hql+=" or labeling=?";
				}
			}
			List<SysMenu> list = sysmenuDao.getSysMenuList(hql, values);
			c.setMenus(new HashSet<SysMenu>(list));
			competenceDao.update(c);
		}
		return null;
	}

	public CompetenceDao getCompetenceDao() {
		return competenceDao;
	}

	public void setCompetenceDao(CompetenceDao competenceDao) {
		this.competenceDao = competenceDao;
	}

	public SysMenuDao getSysmenuDao() {
		return sysmenuDao;
	}

	public void setSysmenuDao(SysMenuDao sysmenuDao) {
		this.sysmenuDao = sysmenuDao;
	}

}
